package com.chensi.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

public class Receiver {
	@Test
	public void test() throws JMSException {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD,
				"tcp://127.0.0.1:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination=session.createQueue("queue1");
		MessageConsumer messageConsumer=session.createConsumer(destination);
		while(true){
			TextMessage msg=(TextMessage) messageConsumer.receive();//阻塞的receive
			if(msg==null){
				break;
			}
			System.out.println(msg.getText());
		}
		if(connection!=null){
			connection.close();
		}
	}
}
